package com.fr.dialog;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;

class ParenMatcher extends JTextPane
  implements Runnable
{
  public static Color[] matchColor = { Color.black, Color.orange, Color.blue };
  public static Color keywordsColor = Color.BLUE;
  public static Color badColor = Color.red;
  public static Color orgColor = Color.black;
  private AttributeSet[] matchAttrSet;
  private MutableAttributeSet badAttrSet;
  private MutableAttributeSet keyAttrSet;
  private MutableAttributeSet orgAttrSet;
  private String[] keywords = { "arguments", "break", "delete", "function", "return", "typeof", "case", "do", "if", "switch", "var", "catch", "else", "in", "this", "void", "continue", "false", "instanceof", "throw", "while", "debugger", "finally", "new", "true", "with", "default", "for", "null", "try" };
  private List keyList;
  private List list = new ArrayList();

  public ParenMatcher()
  {
    StyleContext localStyleContext = StyleContext.getDefaultStyleContext();
    this.badAttrSet = new SimpleAttributeSet();
    StyleConstants.setForeground(this.badAttrSet, badColor);
    StyleConstants.setBold(this.badAttrSet, true);
    StyleConstants.setFontFamily(this.badAttrSet, "DialogInput");
    this.matchAttrSet = new AttributeSet[matchColor.length];
    for (int i = 0; i < matchColor.length; ++i)
      this.matchAttrSet[i] = localStyleContext.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, matchColor[i]);
    this.keyAttrSet = new SimpleAttributeSet();
    StyleConstants.setForeground(this.keyAttrSet, keywordsColor);
    StyleConstants.setFontFamily(this.keyAttrSet, "DialogInput");
    this.orgAttrSet = new SimpleAttributeSet();
    StyleConstants.setForeground(this.orgAttrSet, orgColor);
    StyleConstants.setFontFamily(this.orgAttrSet, "DialogInput");
    this.keyList = new ArrayList();
    for (i = 0; i < this.keywords.length; ++i)
      this.keyList.add(this.keywords[i]);
  }

  public void run()
  {
    StyledDocument localStyledDocument = getStyledDocument();
    String str1 = "";
    int i = localStyledDocument.getLength();
    try
    {
      str1 = localStyledDocument.getText(0, i);
    }
    catch (BadLocationException localBadLocationException)
    {
    }
    Stack localStack = new Stack();
    this.list.clear();
    for (int j = 0; j < str1.length(); ++j)
    {
      hightKeywords(localStyledDocument, str1, j);
      char c = str1.charAt(j);
      if ((c == '(') || (c == '[') || (c == '{'))
      {
        int l = localStack.size();
        localStack.push("" + c + j);
        AttributeSet localAttributeSet1 = this.matchAttrSet[(l % this.matchAttrSet.length)];
        localStyledDocument.setCharacterAttributes(j, 1, localAttributeSet1, false);
      }
      if ((c == ')') || (c == ']') || (c == '}'))
      {
        String str3 = (localStack.empty()) ? "." : (String)localStack.peek();
        if (matches(str3.charAt(0), c))
        {
          localStack.pop();
          int i1 = localStack.size();
          AttributeSet localAttributeSet2 = this.matchAttrSet[(i1 % this.matchAttrSet.length)];
          localStyledDocument.setCharacterAttributes(j, 1, localAttributeSet2, false);
        }
        else
        {
          localStyledDocument.setCharacterAttributes(j, 1, this.badAttrSet, false);
        }
      }
    }
    while (!(localStack.empty()))
    {
      String str2 = (String)localStack.pop();
      int k = Integer.parseInt(str2.substring(1));
      localStyledDocument.setCharacterAttributes(k, 1, this.badAttrSet, false);
    }
  }

  private void hightKeywords(StyledDocument paramStyledDocument, String paramString, int paramInt)
  {
    Pattern localPattern = Pattern.compile("[a-zA-Z]");
    String str1 = "" + paramString.charAt(paramInt);
    Matcher localMatcher = localPattern.matcher(str1);
    boolean bool = localMatcher.matches();
    if (bool)
    {
      this.list.add(str1);
    }
    else
    {
      String str2 = toString(this.list);
      if (this.keyList.contains(str2))
        paramStyledDocument.setCharacterAttributes(paramInt - str2.length(), str2.length(), this.keyAttrSet, false);
      else
        paramStyledDocument.setCharacterAttributes(paramInt - str2.length(), str2.length(), this.orgAttrSet, false);
      this.list.clear();
    }
  }

  private String toString(List paramList)
  {
    Iterator localIterator = paramList.iterator();
    StringBuffer localStringBuffer = new StringBuffer();
    while (localIterator.hasNext())
      localStringBuffer.append((String)localIterator.next());
    return localStringBuffer.toString();
  }

  public void replaceSelection(String paramString)
  {
    getInputAttributes().removeAttribute(StyleConstants.Foreground);
    super.replaceSelection(paramString);
  }

  public static boolean matches(char paramChar1, char paramChar2)
  {
    if (paramChar1 == '(')
      return (paramChar2 == ')');
    if (paramChar1 == '[')
      return (paramChar2 == ']');
    if (paramChar1 == '{')
      return (paramChar2 == '}');
    return false;
  }
}